﻿using JNPF.Basics.Logic.Extend.DocumentPreview;
using JNPF.Basics.Models.Extend.Dtos.DocumentPreview;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;

namespace JNPF.Basics.Controller.Extend
{
    /// <summary>
    /// 文件预览
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21
    /// </summary>
    [Route("api/Extend/[controller]")]
    public class DocumentPreviewController : BaseApiController
    {
        private DocumentPreviewBll DocumentPreviewBll = new DocumentPreviewBll();

        #region Get
        /// <summary>
        /// 获取文档列表
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpGet]
        public ResponseResult GetList([FromQuery] KeywordInput input)
        {
            var output = DocumentPreviewBll.GetList(input);
            return Success(output);
        }

        /// <summary>
        /// 文件在线预览
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns></returns>
        [HttpGet("{fileId}/Preview")]
        public ResponseResult DocumentPreview(string fileId)
        {
            var output = new DocumentPreviewPreviewOutput();
            var filePath = ConfigurationKey.DocumentPreviewFilePath;
            var files = FileHelper.GetAllFiles(filePath);
            if (fileId.ToInt() > files.Count)
            {
                return Error("文件找不到!");
            }
            var file = files[fileId.ToInt()];
            if (file != null)
            {
                output.fileName = file.Name;
                output.filePath = "/api/Extend/DocumentPreview/down/" + file.Name;
                return Success(output);
            }
            return Error("文件找不到!");
        }

        [HttpGet("down/{fileName}")]
        public void FileDown(string fileName)
        {
            var filePath = ConfigurationKey.DocumentPreviewFilePath + fileName;
            var systemFilePath = ConfigurationKey.SystemFilePath + fileName;
            if (FileHelper.Exists(filePath))
                FileHelper.DownloadFile(filePath, fileName);
            else
                FileHelper.DownloadFile(systemFilePath, fileName);
        }
        #endregion
    }
}
